<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
    var reloadTestSuccess = false;
</script>

<script>
    test(function () {
        assert_true(window.internals !== null);
    }, "window.internals is required for the test to run");

    internals.settings.setDisallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections(true);
    internals.setNetworkConnectionInfoOverride(true, 'cellular2g', '4g', 100, 1.0);
    internals.evictAllResources();

    window.addEventListener('beforeunload', function() {
        internals.settings.setDisallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections(false);
        internals.clearNetworkConnectionInfoOverride();
        // Remove localStorage items, just in case they haven't been
        // already removed, due to test failure.
        if (window.localStorage.getItem("errorCount") !== null) {
            window.localStorage.removeItem("errorCount");
        }

        if (window.localStorage.getItem("successCount") !== null) {
            window.localStorage.removeItem("successCount");
        }
    }, false);

    if (window.localStorage.getItem("errorCount") === null) {
        window.localStorage.setItem("errorCount", 0);
    }

    if (window.localStorage.getItem("successCount") === null) {
        window.localStorage.setItem("successCount", 0);
    }

    var crossOrigin = 'http://localhost:8000';
    var filePath = '/loading/resources/js-loaded.js?reload';

    // First time the script will be blocked and onError will reload the page.
    // On reload the script should not be blocked.
    // This tests two types of reload, one with and one without cache bypass.
    // The script should not be blocked in both cases.
    src = crossOrigin + filePath;
    document.write('<scr' + 'ipt src="' + src + '" onload="test(successTest,\'Load Success\')" onError="test(errorTest,\'Load Error\')"></scr' + 'ipt>');

    var successTest = function() {
        assert_equals(+window.localStorage.getItem("errorCount"), 1);

        var successCount = +window.localStorage.getItem("successCount");
        assert_greater_than_equal(successCount, 0);

        if (successCount == 0)
        {
            window.localStorage.setItem("successCount", 1);
            internals.forceReload(true);
            return;
        }

        assert_equals(successCount, 1);
        window.localStorage.removeItem("errorCount");
        window.localStorage.removeItem("successCount");
        reloadTestSuccess=true;
    }

    var errorTest = function() {
        assert_equals(+window.localStorage.getItem("errorCount"), 0);
        assert_equals(+window.localStorage.getItem("successCount"), 0);
        window.localStorage.setItem("errorCount", 1);
        internals.forceReload(false);
    }

</script>

<script>
    test(function () {
        assert_true(reloadTestSuccess);
    }, "cross origin doc.written scripts are not blocked in a page reload");
</script>
